package com.example.idachuappone.order.imageutil;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.ImageView;

import com.example.idachuappone.order.item.SubmitCommentActivity;

public class BitmapCache extends Activity {
	public final String TAG;
	public Handler h;
	private HashMap<String, SoftReference<Bitmap>> imageCache;

	class AnonymousClass_1 extends Thread {
		Bitmap thumb;
		private final/* synthetic */ImageCallback val$callback;
		private final/* synthetic */boolean val$isThumbPath;
		private final/* synthetic */ImageView val$iv;
		private final/* synthetic */String val$path;
		private final/* synthetic */String val$sourcePath;
		private final/* synthetic */String val$thumbPath;

		class AnonymousClass_2 implements Runnable {
			private final/* synthetic */ImageCallback val$callback;
			private final/* synthetic */ImageView val$iv;
			private final/* synthetic */String val$sourcePath;

			AnonymousClass_2(ImageCallback imageCallback, ImageView imageView, String str) {
				this.val$callback = imageCallback;
				this.val$iv = imageView;
				this.val$sourcePath = str;
			}

			@Override
			public void run() {
				this.val$callback.imageLoad(this.val$iv, AnonymousClass_1.this.thumb, this.val$sourcePath);
			}
		}

		AnonymousClass_1(boolean z, String str, String str2, String str3, ImageCallback imageCallback,
				ImageView imageView) {
			this.val$isThumbPath = z;
			this.val$thumbPath = str;
			this.val$sourcePath = str2;
			this.val$path = str3;
			this.val$callback = imageCallback;
			this.val$iv = imageView;
		}

		@Override
		public void run() {
			try {
				if (this.val$isThumbPath) {
					this.thumb = BitmapFactory.decodeFile(this.val$thumbPath);
					if (this.thumb == null) {
						this.thumb = BitmapCache.this.revitionImageSize(this.val$sourcePath);
					}
				} else {
					this.thumb = BitmapCache.this.revitionImageSize(this.val$sourcePath);
				}
			} catch (Exception e) {
			}
			if (this.thumb == null) {
				this.thumb = SubmitCommentActivity.bimap;
			}
			BitmapCache.this.put(this.val$path, this.thumb);
			if (this.val$callback != null) {
				BitmapCache.this.h.post(new AnonymousClass_2(this.val$callback, this.val$iv, this.val$sourcePath));
			}
		}
	}

	public static interface ImageCallback {
		void imageLoad(ImageView imageView, Bitmap bitmap, Object... objArr);
	}

	public BitmapCache() {
		this.h = new Handler();
		this.TAG = getClass().getSimpleName();
		this.imageCache = new HashMap();
	}

	public void put(String path, Bitmap bmp) {
		if (!TextUtils.isEmpty(path) && bmp != null) {
			this.imageCache.put(path, new SoftReference(bmp));
		}
	}

	public void displayBmp(ImageView iv, String thumbPath, String sourcePath, ImageCallback callback) {
		if (!TextUtils.isEmpty(thumbPath) || !TextUtils.isEmpty(sourcePath)) {
			String path;
			boolean isThumbPath;
			if (!TextUtils.isEmpty(thumbPath)) {
				path = thumbPath;
				isThumbPath = true;
			} else if (TextUtils.isEmpty(sourcePath)) {
				iv.setImageBitmap(null);
				return;
			} else {
				path = sourcePath;
				isThumbPath = false;
			}
			if (this.imageCache.containsKey(path)) {
				Bitmap bmp = (Bitmap) ((SoftReference) this.imageCache.get(path)).get();
				if (bmp != null) {
					if (callback != null) {
						callback.imageLoad(iv, bmp, sourcePath);
					}
					iv.setImageBitmap(bmp);
					return;
				}
			}
			iv.setImageBitmap(null);
			new AnonymousClass_1(isThumbPath, thumbPath, sourcePath, path, callback, iv).start();
		}
	}

	public Bitmap revitionImageSize(String path) throws IOException {
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
		Options options = new Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(in, null, options);
		in.close();
		int i = 0;
		while (true) {
			if ((options.outWidth >> i) > 256 || (options.outHeight >> i) > 256) {
				i++;
			} else {
				in = new BufferedInputStream(new FileInputStream(new File(path)));
				options.inSampleSize = (int) Math.pow(2.0d, i);
				options.inJustDecodeBounds = false;
				return BitmapFactory.decodeStream(in, null, options);
			}
		}
	}
}
